package com.itheima.controller;

import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import com.itheima.pub.MessageConstant;
import com.itheima.service.MenuService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Reference
    MenuService menuService;

    @RequestMapping("getMenus")
    public Result getMenus(Authentication authentication){
        try {
            String username = authentication.getName();
            Set<Menu> list = menuService.getMenusByUsername(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    // 分页查询检查项
    @RequestMapping("findPage.do")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Map page = menuService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    // 添加或者更新检查项
    @RequestMapping("add.do")
    public Result addCheckItem(@RequestBody Menu menu){
        try {
            // saveOrUpdate 此方法主键值为null或者为0为插入,否则为更新
            menuService.saveOrUpdate(menu);
            return new Result(true, MessageConstant.UPDATE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_MENU_FAIL);
        }
    }

    // 根据id值删除检查项
    @RequestMapping("deleteMenuById.do")
    public Result deleteCheckitemById(@RequestParam("id") Integer id){
        try {
            Menu menu = new Menu();
            menu.setId(id);
            // 目前来看此种方法最适合进行逻辑删除  值为null的不更新,  那为0或者0.0呢??(price变量 属性为float 为什么不更新)
            menuService.removeById(menu);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }

    // 这是用在角色页面新建及编辑检查组用来显示所有菜单的方法
    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<Menu> list = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
}
